package com.cgnpc.scp.common.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.net.URL;

/**
 * app的相关配置信息
 */
@Service
public class AppConstantService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AppConstantService.class);

    // downloadWebPathRoot文件下载路径根目录
    @Value("${custom.downloadWebPathRoot:}")
    private String customDownloadWebPathRoot;

    protected String getStaticCreateFileDir() {
        return "static/___scpTempDir/___tempCreateFileDir/";
    }

    protected String getStaticUploadCreateFileDir() {
        return "static/___scpTempDir/___tempUploadFileDir/";
    }

    /**
     * 创建文件的临时目录
     *
     * @return 临时路径，末尾包括"/"，使用时请注意
     */
    public String getTempCreateFileDir() {
        // return "/root/___scpTempDir/___tempCreateFileDir/";
        try {
            URL classLoaderUrl = AppConstantService.class.getClassLoader().getResource("");
            return new File(classLoaderUrl.getFile(), getStaticCreateFileDir()).getCanonicalPath();
        } catch (Exception e) {
            LOGGER.error("error;", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 上传文件的临时目录
     *
     * @return 临时目录，末尾包括"/"，使用时请注意
     */
    public String getTempUploadFileDir() {
        // return "/root/___scpTempDir/___tempUploadFileDir/";
        try {
            URL classLoaderUrl = AppConstantService.class.getClassLoader().getResource("");
            return new File(classLoaderUrl.getFile(), getStaticUploadCreateFileDir()).getCanonicalPath();
        } catch (Exception e) {
            LOGGER.error("error;", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取服务器的临时文件路径
     */
    public String getWebTempFileRootUrl() {
        // http://localhost:8089/static/
        String usedCustomDownloadWebPathRoot = this.customDownloadWebPathRoot;
        usedCustomDownloadWebPathRoot = usedCustomDownloadWebPathRoot.endsWith("/") ? usedCustomDownloadWebPathRoot : usedCustomDownloadWebPathRoot + "/";
        return usedCustomDownloadWebPathRoot + getStaticCreateFileDir();
    }

    public String getHeaderNameWithNeedWriteLocalFile() {
        return "___needWriteLocalFile";
    }

}
